package com.kevis.zhunblog.admin.controller.modules.article;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kevis.webCommon.web.domain.response.Result;
import com.kevis.zhunblog.admin.controller.base.BaseController;
import com.kevis.zhunblog.core.dto.PageDomain;
import com.kevis.zhunblog.core.dto.query.CommonInfoQuery;
import com.kevis.zhunblog.core.entities.ArticleEntity;
import com.kevis.zhunblog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.Map;

import static com.kevis.webCommon.web.domain.response.Result.decide;
import static com.kevis.webCommon.web.domain.response.Result.failure;

/**
 * 文章控制器
 *
 * @Author kevis
 * @Date 2023/4/18 16:15
 */
@RestController
@RequestMapping("/article/info/")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;

    public ArticleController() {
        Module_Name = "modules/article/info/";
        TableName = "article";
        pageTitle = "资讯";
    }

    @GetMapping("{template}/main")
    public ModelAndView main(@PathVariable(name = "template", required = false) String template) {
        return jumpPage(Module_Name + template + "/main");
    }

    /**
     * 获取分页列表数据
     *
     * @return
     */
    @PostMapping("{template}/list")
    public Object pagelist(@PathVariable(name = "template", required = false) String template
            , @RequestParam("page") Integer currpage) {
        CommonInfoQuery query = new CommonInfoQuery();
        //query.setSearchParentId(new StringKevis(request.getParameter("SearchParentId")).defaultIsNullOrEmpty(0));
        //query.setSearchStatus(new StringKevis(request.getParameter("SearchMarkStatus")).defaultIsNullOrEmpty(9999));
        query.setSearchName(request.getParameter("SearchName"));
        query.setSearchValue(request.getParameter("SearchValue"));
//        query.setSearchTimeType(new StringKevis(request.getParameter("SearchTimeType")).defaultIsNullOrEmpty(0));
//        query.setSearchTimeBegin(new StringKevis(request.getParameter("SearchTimeBegin")).defaultIsNullOrEmpty(LocalDateTime.now().plusYears(-1)));
//        query.setSearchTimeEnd(new StringKevis(request.getParameter("SearchTimeEnd")).defaultIsNullOrEmpty(LocalDateTime.now()));

        PageDomain pageDomain = new PageDomain();
        //PageInfo<ArticleEntity> pageList = articleService.getPageList(query, pageDomain);

        Page<ArticleEntity> page = new Page<ArticleEntity>(currpage, 10).addOrder(new OrderItem("ID", false));
        IPage<ArticleEntity> pageList = articleService.getPageList(query, page);
        if ("easyui".equalsIgnoreCase(template))
            return pageEasyuiTable(pageList.getRecords(), pageList.getTotal());
        return pageTable(pageList.getRecords(), pageList.getTotal());
//        Cache ca = cacheManager.getCache("ca");
//        if (ca != null) {
//            Cache.ValueWrapper bb = ca.get("getPageList");
//            if (bb != null) {
//                System.out.println(bb.get());
//            }
//        }
//        if ("easyui".equalsIgnoreCase(template))
//            return pageEasyuiTable(pageList.getList(), pageList.getTotal());
//        return pageTable(pageList.getList(), pageList.getTotal());
    }

    @GetMapping("{Template}/add")
    public ModelAndView add(@PathVariable String Template) {
        ArticleEntity entity = new ArticleEntity();
        ModelAndView modelAndView = jumpPage(Module_Name + Template + "/edit");
        modelAndView.addObject("model", entity);
        //modelAndView.addObject("parentClassUrl", "'?TableName=" + TableName + "&parentId=0&isRoot=true&IsAllStatus=false&parentId=0'");
        return modelAndView;
    }

    @PostMapping("{Template}/add")
    public Result save(@PathVariable String Template
            , @RequestParam Map<String, Object> params
            , @Validated ArticleEntity articleEntity)
    {
        if (articleEntity.getClassId() == null) {
            return failure("请选择所属栏目");
        }

        articleEntity.setCreateTime(LocalDateTime.now());
        articleEntity.setStatus((byte) 0);
        articleEntity.setAdminId(userContext.getUserId());
        boolean result = articleService.save(articleEntity);
        return decide(result);
    }
}
